"use strict";
//状态字典
var statusDict = [
    {value:'1',name:'启用'},
    {value:'0',name:'停用'}
];
var dataGridObj = {
    $dg: undefined,
    init: function () {
        var total = 0;
        var pageSize = 20;
        var pageNum = 1;
        if (this.$dg == undefined) {
            // 设置table选择器，用来生成datagrid
            this.$dg = $('#dataGridTable');
        }
        if (this.$dg.length > 0) {
            this.$dg.datagrid({
                // 响应式布局
                url:contextPath+'/console/scheme/loadSchemeByPage?pageNum=1&pageSize=10',
                loadMsg: '加载中...',
                fit: true,
                columns: [[
                    {field:'schemeCode',title:'方案代码',width:'20%',align:'center',hidden:'true'},
                    {field:'title',title:'方案标题',width:'20%',align:'left',halign: 'center' },
                    {field:'status',title:'是否启用',width:'20%',align:'center',
                        formatter: function (value, row, index) {
                            return '<input id="'+row.schemeCode+'" />';
                        }
                    }
                ]],
                rownumbers: true,
                rownumberWidth: 70,
                singleSelect: true,
                emptyMsg:'暂无数据',
                pagination:true,
                pageSize:pageSize,
                pageList:[5,10,20,40,80],
                toolbar: [{
                    id:"easyuiAddBtn",
                    text: '新增',
                    iconCls: 'icon-add',
                    plain:true,
                    handler: function () {
                        dataGridObj.goAdd();
                    }
                },{
                    id:"easyuiUptBtn",
                    text: '编辑',
                    iconCls: 'icon-edit',
                    plain:true,
                    handler: function () {
                        dataGridObj.goUpdate();
                    }
                },{
                    id:"easyuiDelBtn",
                    text: '删除',
                    iconCls: 'icon-remove',
                    plain:true,
                    handler: function () {
                        dataGridObj.doDelete();
                    }
                },{
                    id:"easyuiSchemeBtn",
                    text:'拓扑图维护',
                    iconCls:'icon-large-chart',
                    plain:true,
                    handler: function () {
                        dataGridObj.goSchemeDraw();
                    }
                }],
                onLoadSuccess:function (data) {
                    total = dataGridObj.$dg.datagrid('getData').total;
                    var pager = dataGridObj.$dg.datagrid('getPager');
                    pager.pagination({
                        onSelectPage: function (pageNumber, size) {
                            pageSize = size;
                            pageNum = pageNumber == 0 ?1:pageNumber;
                            var url = contextPath+'/console/scheme/loadSchemeByPage?pageSize='+size+'&pageNum='+pageNum;
                            $.get(
                                url,
                                {},
                                function (data) {
                                    dataGridObj.$dg.datagrid('loadData', data);
                                },
                                'json'
                            );
                        }
                    });
                    //判断运维用户是否有读写权限，开关工具栏功能按钮
                    toolbarSwitch();

                    data.rows.forEach(function(value, index, array) {
                        renderSwitchBtn(value);
                    });
                }
            });
            // 重新赋值：获取渲染后的datagrid
            this.$dg = $('#dataGridTable');
            // 渲染成功，返回true
            return true;
        }
        return false;
    },
    // 新增监控方案，调Dialog打开方法，并且可编辑
    goAdd: function () {
        console.log("xx");
        dialogObj.openAdd(contextPath + '/console/scheme/add');
    },
    // 修改监控方案，调Dialog打开方法，并且可编辑
    goUpdate: function () {
        dialogObj.openUpdate(this.$dg.datagrid('getSelected'),contextPath + '/console/scheme/update');
    },
    // 删除监控方案
    doDelete: function () {
        // 获取选中的行
        var row = this.$dg.datagrid('getSelected');
        if (row) {
            apexLayer.Confirm("您确认删除监控方案吗？",
                "删除监控方案",
                "确认",
                "取消",
                function () {
                    Ajax({
                        url: contextPath + "/console/scheme/delete",
                        isMask: true,
                        data: {
                            "id": row.id
                        },
                        success: function (data) {
                            if (data.code>0) {
                                apexLayer.Msg(data.note, 1000, function () {
                                    dataGridObj.$dg.datagrid("reload");
                                });
                            } else {
                                apexLayer.Error("删除失败:" + data.note);
                            }
                        }
                    });
                }
            );
        } else {
            apexLayer.Warn('请先选择一条记录，才能进行删除操作！');
        }
    },
    //跳转至拓扑图维护界面
    goSchemeDraw:function () {
        var row = this.$dg.datagrid('getSelected');
        if (!row) {
            apexLayer.Error('请先选择一条记录，才能进行操作！');
            return;
        }
        var schemeCode = row.schemeCode;
        var schemeTitle = row.title;
        window.location.href = contextPath +"/console/scheme/goSchemeDraw?schemeCode="+schemeCode+"&schemeTitle="+schemeTitle;
    }
};

$(function () {
    dataGridObj.init();
    dialogObj.init("add-dlg","dataForm");
    // 渲染Dialog
    $('#add-dlg').dialog({
        width: 400,
        height: 'auto',
        closed: true,
        cache: false,
        modal: true,
        shadow: false,
        buttons: [{
            text: '保存',
            iconCls: 'icon-save',
            handler: function () {
                dialogObj.submiHandler("dataGridTable");
            }
        }],
        onClose: function () {
        // 还原Dialog
        dialogObj.reset();
        }
    });
});

function searchByCondition(num,size) {
    var schemeName = $("#search_schemeName").val();
    var pageNum = num==null||num==undefined?1:num;
    var pageSize = size==null||size==undefined?15:size;
    var url = contextPath+'/console/scheme/loadSchemeByPage';
    $.get(
        url,
        {
            schemeName:schemeName,
            pageNum:pageNum,
            pageSize:pageSize
        },
        function (data) {
            dataGridObj.$dg.datagrid('loadData', data);
        },
        'json'
    );
}

function resetCondition() {
    $('#search_schemeName').textbox('setValue', "");
    searchByCondition();
}


function renderSwitchBtn(value) {
    $("#"+value.schemeCode).switchbutton({
        width:50,
        height:20,
        onText:'ON',
        offText:'OFF',
        checked:value.status!="0",
        onChange:function(checked){
            console.log(checked);
            setTimeout(function(){
                var row = dataGridObj.$dg.datagrid('getSelected');
                var rowIndex = dataGridObj.$dg.datagrid('getRowIndex',row);
                row.status = checked?"1":"0";
                console.log(row);
                Ajax({
                    url:contextPath+"/console/scheme/update",
                    data:row,
                    success:function(data){
                        console.log(data);
                        if(data.code>0){
                            dataGridObj.$dg.datagrid("reload");
                        }else{
                            apexLayer.Error(data.note);
                        }
                    }
                });
            },100);
        }
    });
}